package mongo.db.clients.firsts;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class Update {

	public static void main(String[] args) {
		
		
		List<ServerAddress> seeds = null;
		MongoClient client = null;

		DB db = null;
		DBCollection collection = null;
		
		
		seeds = new ArrayList<ServerAddress>();

		try {
			seeds.add(new ServerAddress("202.191.136.234", 27017));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		client = new MongoClient(seeds);

		db = client.getDB("dopinventoryupsert");
		collection = db.getCollection("devicesupsert");
		
		int i = 0;
		
		try {
			BufferedReader br = new BufferedReader(new FileReader("/tmp/dop/deviceList.csv"));
			String line;
			while ((line = br.readLine()) != null) {
//				System.out.print("\r" + line.split(",")[0] + ":" + line.split(",")[2] + "-" + line.split(",")[3] );
				System.out.print("\r" + ++i);
				String weg = line.split(",")[0];
				String ip = line.split(",")[1];
				String username = line.split(",")[2];
				String password = line.split(",")[3];

				DBObject find = new BasicDBObject("_id", ip);
				DBObject update = new BasicDBObject("$set", new BasicDBObject().append("config.telnet_username", username).append("config.telnet_password", password));
				
				collection.update(find, update, true, false);
				
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		

	}

}
